<?php
class It_ProjectController extends Zend_Controller_Action
{
	private $_config;
    public function init ()
    {
        $this->_config = new Zend_Config_Ini(CONFIG_PATH . '/rolesDefault.ini',APPLICATION_ENV);
        $acl = new My_Acl();
        $auth = $acl->checkAllowed($this->_request);
    }

    public function indexAction ()
    {
        $model_project = new It_Model_Project();
        $this->view->project_list = $model_project->getAllData();
    }

    public function deleteAction ()
    {
        $model_project = new It_Model_Project();
        $id = (int)($this->_getParam('id', 0));
        $checkProject = $model_project->checkProject($id)->toArray();
        if($checkProject){
            $this->view->mess = 'Dự án này vẫn còn thiết bị. Vui lòng chuyển thiết bị qua dự án khác!!!';
        }else{
            if($this->getRequest()->isPost()){
                $del = $this->getRequest()->getPost('del');
                if ($del == 'Có') {
                    $projectId = $this->getRequest()->getPost('projectId');
                    $model_project->deleteData($projectId);
                }
                $this->_helper->redirector('index','project','it');
            }else{
                $this->view->project = $model_project->getDataById($id);
            }
        }
    }

    public function addAction ()
    {
        $form = new It_Form_Project();
        $form->submit->setLabel('Thêm mới!');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)){
                
                $projectName = $form->getValue('projectName');
                $note = $form->getValue('note');
                
                $auth = Zend_Auth::getInstance();
                $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB_IT'));
                $userInfo = $auth->getIdentity();
                $model_users = new It_Model_Users();
                $users_check = $model_users->getUserByUsername($userInfo['username']);
                
                $model_project = new It_Model_Project();
                $idProject = $model_project->addData($projectName, $note, $users_check->userId);
                
                $this->_helper->redirector('index','project','it');
            }else{
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
		$form = new It_Form_Project();
		$form->submit->setLabel('Lưu');
		$this->view->form = $form;

		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$projectId = (int) $form->getValue('projectId');
				$projectName = $form->getValue('projectName');
				$note = $form->getValue('note');
                
				$model_project = new It_Model_Project();
				$model_project->updateData($projectId, $projectName, $note);

				$this->_helper->redirector('index','project','it');
			}else{
				$form->populate($formData);
			}
		}else{
			$id = (int)($this->_getParam('id', 0));
            
			$model_project = new It_Model_Project();
            $project_check = $model_project->getDataById($id);
			$form->populate($project_check->toArray());
		}
    }
}





